package com.mysoft.gfyx.marketdevelop.controller.dto.inspect;

import com.mysoft.gfyx.marketdevelop.mip.dto.TjCustom.SqlApiResponseDto;
import com.mysoft.gfyx.marketdevelop.mip.dto.TjMetric.QueryMetricResponseDto;
import lombok.Data;

import java.io.Serializable;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@Data
public class MultiPieResponseDto implements Serializable {

    private Map<String, List<Item>> data;

    public MultiPieResponseDto() {
        data = new HashMap<>();
    }

    @Data
    public static class Item implements Serializable {
        private String name;
        private Double value;
    }


    public void loadData(SqlApiResponseDto dto, String tag, String[] metrics) {
        for (int i = 0; i < metrics.length; i++) {
            String field = metrics[i];
            List<Item> pie = new LinkedList<>();
            dto.getData().getRows().forEach(r -> {
                Item item = new Item();
                item.setName(r.get(tag).toString());
                item.setValue(Double.valueOf(r.get(field).toString()));
                pie.add(item);
            });

            data.put(field, pie);
        }
    }
}
